import axios from "axios";
import { CreateAgendaCmd, UpdateAgenda } from '../../dto/agenda';
import { deleteTableRecords, getTableRecordsSearch, saveTableRecords, updateTableRecords } from "../../infra/BitableUtils";
import { FeiShuTable } from "../../infra/util/TableUtils";
import { Condition, Filter, FSBitableQuery, QUERTY_CONJUNCTION, QUERTY_OPERATOR, Sort } from "../../infra/queryObj/ConditionalBitableQuery";

//TODO 时间戳暂放?
export async function getAgenda(timestamp:number){
    const query: FSBitableQuery = {
        field_names: ["id", "name", "description", "start_time", "end_time", "time_detail", "is_important"],
        filter: {
            conjunction: QUERTY_CONJUNCTION.AND,
            conditions: [
                {
                    field_name: "start_time",
                    value: [String(timestamp)],
                    operator: QUERTY_OPERATOR.IS_GREATER_EQUAL
                },
                /*{
                    field_name: "endTime",
                    value: [String(timestamp)],
                    operator: QUERTY_OPERATOR.IS_LESS_EQUAL
                }*/
            ]
        },
    };
    
      return getTableRecordsSearch(FeiShuTable.Agenda, query, "");

}

export async function createAgenda(agenda:CreateAgendaCmd){
    const result=saveTableRecords(FeiShuTable.Agenda,{
        name:agenda.name,
        description:agenda.description,
        startTime:agenda.startTime,
        endTime:agenda.endTime,
        isImportant:agenda.isImportant,
        timeDetail:agenda.timeDetail
    });
    return result;
}

export async function deleteAgenda(id:number){
    const result=deleteTableRecords(FeiShuTable.Agenda,id.toString()); 
    return result;
}

export async function updateAgenda(agenda:UpdateAgenda){
    const result=updateTableRecords(FeiShuTable.Agenda,{
        name:agenda.name,
        description:agenda.description,
        startTime:agenda.startTime,
        endTime:agenda.endTime,
        isImportant:agenda.isImportant,
        timeDetail:agenda.timeDetail
    },agenda.id);
    return result;
}
